package com.ptc.ims.client.presenter;

import com.google.gwt.event.shared.HandlerManager;
import com.ptc.ims.client.InventoryServiceAsync;
import com.ptc.ims.client.view.ProductEditView;
import com.ptc.ims.client.view.ProductListView;
import com.ptc.ims.client.view.ProductCreateView;

public class ProductPresenterFactory 
{
	/**
	 * 
	 * @param presenter
	 * @return null when no presenter is found
	 */
	public static Presenter createPresenter(String token, InventoryServiceAsync rpcService, HandlerManager eventBus)
	{
		Presenter presenter = null;
		
		if (ProductListPresenter.PAGE.equals(token))
		{
			presenter = new ProductListPresenter(rpcService, eventBus, new ProductListView());
		}
		else if (ProductPresenter.PAGE_CREATE.equals(token))
		{
			presenter =  new ProductPresenter(rpcService, eventBus, new ProductCreateView());
		}
		else if (token != null && token.contains(ProductPresenter.PAGE_EDIT))
		{
			String[] tokens = token.split("&productId=");
			if (tokens.length == 2)
			{
				presenter =  new ProductPresenter(rpcService, eventBus, new ProductEditView(tokens[1]));
			}
			else
			{
				presenter =  new ProductPresenter(rpcService, eventBus, new ProductEditView(null));
			}
			
		}
		
		return presenter;
	}
}
